package com.caipin.controller;

/**
 * @Author: zhunian0322@163.com
 * @DateTime: 2021-09-30 14:57
 * @Description: TODO
 */
@RestController
public class SenseNebulaHttpController {
    private static final Logger log = LogManager.getLogger(com.caipin.controller.SenseNebulaHttpController.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Autowired
    private CameraPlatformManagementService cameraPlatformManagementService;

    @RequestMapping({"/viper/event"})
    Object event(HttpServletRequest request) {
        MultipartHttpServletRequest params = (MultipartHttpServletRequest)request;
        String json = params.getParameter("json");
        try {
            Object data;
            Map map_data;
            Integer libType, similarity;
            String trigger, camera_name, device_id;
            int personType;
            Map map = (Map)this.objectMapper.readValue(json, Map.class);
            Integer msg_id = (Integer)map.get("msg_id");
            switch (msg_id.intValue()) {
                case 774:
                    data = map.get("data");
                    map_data = (Map)this.objectMapper.convertValue(data, Map.class);
                    libType = (Integer)map_data.get("lib_type");
                    similarity = (Integer)map_data.get("similarity");
                    trigger = (String)map_data.get("trigger");
                    camera_name = (String)map_data.get("camera_name");
                    device_id = (String)map_data.get("device_id");
                    personType = -1;
                    if (libType.intValue() == 0) {
                        log.info("" + trigger + " " + camera_name);
                        personType = 0;
                    } else if (libType.intValue() == 1) {
                        if (similarity.intValue() > 75) {
                            log.info("+ map_data.get("person_name") + " " + trigger);
                        } else {
                            log.info("" + trigger + " " + camera_name);
                            personType = 0;
                        }
                    } else if (libType.intValue() == 2) {
                        if (similarity.intValue() > 75) {
                            log.info("" + map_data.get("person_name") + " " + trigger);
                        } else {
                            log.info("" + trigger + " " + camera_name);
                            personType = 0;
                        }
                    }
                    if (personType == 0) {
                        SecurityWarning securityWarning = (new SecurityWarning()).setMark(Boolean.valueOf(false)).setAddress(camera_name).setEvent(");
                        this.cameraPlatformManagementService.saveSecurityWarning(securityWarning);
                        SecurityEvent securityEvent = new SecurityEvent();
                        securityEvent.setDeviceId(device_id);
                        securityEvent.setChannelLocation(camera_name);
                        securityEvent.setSnapTime(trigger);
                        MultipartFile snap_frame = params.getFile("snap_frame");
                        byte[] snap_frameBytes = snap_frame.getBytes();
                        String imageBase64 = Base64.getEncoder().encodeToString(snap_frameBytes);
                        long imageId = this.cameraPlatformManagementService.saveSecurityEventImage(securityEvent, imageBase64);
                        MultipartFile snap = params.getFile("snap");
                        byte[] snapBytes = snap.getBytes();
                        String chip = Base64.getEncoder().encodeToString(snapBytes);
                        SecurityFace securityFace = (new SecurityFace()).setImageId(Long.valueOf(imageId)).setDeviceId(device_id).setSnapTime(trigger).setChip(chip).setFaceStranger(Boolean.valueOf(true)).setMark(Boolean.valueOf(false));
                        this.cameraPlatformManagementService.saveSecurityEventFace(securityFace);
                    }
                    break;
                case 775:
                    log.info(");
                    break;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}

